package bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="processaBean")
@RequestScoped
public class ProcessaBean {
	String url ="jdbc:postgresql://localhost/TesteMeter"; 
	String usuario="postgres"; 
	String senha = "1234";
	Connection con=null;

	
	private String valor;
	private String nome;
	
		public void insereBanco(){
		abreConexao();
		try {			
			Statement stm = con.createStatement();
			stm.executeUpdate("insert into public.pessoa(id, nome) values(1,'"+this.nome+"');");
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void abreConexao() {
		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			con=DriverManager.getConnection(url,usuario,senha);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		 
	}
	

	
	
		
	public void chamaFibo(){
		for(int i=0; i<Long.parseLong(valor);i++){
			System.out.print("(" + i + "):" + fibo(i) + "\n");
		}
	}
	
	public long fibo(int n) {
        if (n < 2) {
            return n;
        } else {
            return fibo(n - 1) + fibo(n - 2);
        }
    }
	
	
	public String getValor() {
		return valor;
	}

	public void setValor(String valor) {
		this.valor = valor;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}
}
